$(document).ready(function () {
  $('form[name="edit_form"]').on("submit", function (e) {
    e.preventDefault();

    $.ajax({
      url: "/account/edit",
      method: "POST",
      data: $(this).serialize(),
      success: function (res) {
        if (res.status === "success") {
          window.location.href = "/account";
        }
      },
    });
  });

  $('form[name="create_form"]').on("submit", function (e) {
    e.preventDefault();

    $.ajax({
      url: "/account/create",
      method: "POST",
      data: $(this).serialize(),
      success: function (res) {
        if (res.status === "success") {
          window.location.href = "/account";
        } else {
          window.location.href = "/account";
          window.alert(res.message);
        }
      },
    });
  });

  $('form[name="edit_form_course"]').on("submit", function (e) {
    e.preventDefault();

    $.ajax({
      url: "/course/edit",
      method: "POST",
      data: $(this).serialize(),
      success: function (res) {
        if (res.status === "success") {
          window.location.href = "/course";
        } else {
          window.location.href = "/course";
          window.alert(res.message);
        }
      },
    });
  });

  $('form[name="create_form_course"]').on("submit", function (e) {
    e.preventDefault();

    $.ajax({
      url: "/course/create",
      method: "POST",
      data: $(this).serialize(),
      success: function (res) {
        if (res.status === "success") {
          window.location.href = "/course";
        } else {
          window.location.href = "/course";
          window.alert(res.message);
        }
      },
    });
  });

  $('form[name="create_form_lecture"]').on("submit", function (e) {
    e.preventDefault();
    let formData = new FormData($(this).get(0));
    $.ajax({
      url: "/lecture/create",
      method: "POST",
      data: formData,
      contentType: false,
      processData: false,
      success: function (res) {
        if (res.status === "success") {
          window.location.href = "/lecture";
        } else if ((res.status = "match_error")) {
          window.location.href = "/lecture";
          window.alert(res.message);
        } else if ((res.status = "error")) {
          window.location.href = "/lecture";
          window.alert(res.message);
        }
      },
    });
  });

  $('form[name="edit_form_lecture"]').on("submit", function (e) {
    e.preventDefault();
    let formData = new FormData($(this).get(0));
    $.ajax({
      url: "/lecture/edit",
      method: "POST",
      data: formData,
      contentType: false,
      processData: false,
      success: function (res) {
        if (res.status === "success") {
          window.location.href = "/lecture";
        } else {
          window.location.href = "/lecture";
          window.alert(res.message);
        }
      },
    });
  });

  $('form[name="edit_form_assignment"]').on("submit", function (e) {
    e.preventDefault();

    $.ajax({
      url: "/assignment/create",
      method: "POST",
      data: $(this).serialize(),
      success: function (res) {
        if (res.status === "success") {
          window.location.href = "/";
        } else {
          window.location.href = "/";
          window.alert(res.message);
        }
      },
    });
  });

  $('form[name="create_form_submission"]').on("submit", function (e) {
    e.preventDefault();
    let formData = new FormData($(this).get(0));
    $.ajax({
      url: "/assignment/student/submit",
      method: "POST",
      data: formData,
      contentType: false,
      processData: false,
      success: function (res) {
        if (res.status === "success") {
          window.location.href = "/assignment/student";
        } else {
          window.location.href = "/assignment/student";
          window.alert(res.message);
        }
      },
    });
  });

  $('form[name="create_form_mark"]').on("submit", function (e) {
    e.preventDefault();

    $.ajax({
      url: "/submission/mark",
      method: "POST",
      data: $(this).serialize(),
      success: function (res) {
        if (res.status === "success") {
          window.location.href = '/assignment/page';
          window.alert(res.message);
        } else {
          window.location.href = '/assignment/page';
          window.alert(res.message);
        }
      },
    });
  });
});
